﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using GMap.NET;
using GMap.NET.WindowsForms;

namespace GMapClient
{
#if !PocketPC
    [Serializable]
    public class GMapMarkerCircle : GMapMarker, ISerializable
#else
    public class GMapMarkerCircle : GMapMarker
#endif
    {
        /// <summary>
        /// In Meters 用米数确定圆
        /// </summary>
        public int Radius;

        /// <summary>
        /// 用两个点确定圆
        /// </summary>
        public PointLatLng P2;

        /// <summary>
        /// 是否用米数确定圆
        /// </summary>
        public bool IsMeter = true;


        /// <summary>
        /// specifies how the outline is painted
        /// </summary>
        [NonSerialized]
#if !PocketPC
        public Pen Stroke = new Pen(Color.FromArgb(155, Color.MidnightBlue));
#else
       public Pen Stroke = new Pen(Color.MidnightBlue);
#endif

        /// <summary>
        /// background color
        /// </summary>
        [NonSerialized]
#if !PocketPC
        public Brush Fill = new SolidBrush(Color.FromArgb(155, Color.AliceBlue));
#else
       public Brush Fill = new System.Drawing.SolidBrush(Color.AliceBlue);
#endif

        /// <summary>
        /// is filled
        /// </summary>
        public bool IsFilled = false;

        public GMapMarkerCircle(PointLatLng p)
           : base(p)
        {
            Radius = 0; // 0m
            P2 = p;
            IsHitTestVisible = false;
        }

        public override void OnRender(Graphics g)
        {
            if (!IsMeter)
                Radius = (int)Overlay.Control.MapProvider.Projection.GetDistance(Position, P2) * 1000;

            int R = (int)((Radius) / Overlay.Control.MapProvider.Projection.GetGroundResolution((int)Overlay.Control.Zoom, Position.Lat)) * 2;
         
            if (IsFilled)
            {
                g.FillEllipse(Fill, new System.Drawing.Rectangle(LocalPosition.X - R / 2, LocalPosition.Y - R / 2, R, R));
            }
            g.DrawEllipse(Stroke, new System.Drawing.Rectangle(LocalPosition.X - R / 2, LocalPosition.Y - R / 2, R, R));

            g.DrawString("8888", new Font("宋体", 20), new SolidBrush(Color.Red),
                               new Point(LocalPosition.X - R / 2, LocalPosition.Y - R / 2));
            //g.DrawRectangle(Stroke, new System.Drawing.Rectangle(LocalPosition.X - R / 2, LocalPosition.Y - R / 2, R, R));

        }

        public override void Dispose()
        {
            if (Stroke != null)
            {
                Stroke.Dispose();
                Stroke = null;
            }

            if (Fill != null)
            {
                Fill.Dispose();
                Fill = null;
            }

            base.Dispose();
        }

        public bool IsInside(PointLatLng p)
        {
            return (int)Overlay.Control.MapProvider.Projection.GetDistance(Position, p) * 1000 < Radius;
        }

#if !PocketPC

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);

            // TODO: Radius, IsFilled
        }

        protected GMapMarkerCircle(SerializationInfo info, StreamingContext context)
           : base(info, context)
        {
            // TODO: Radius, IsFilled
        }

        #endregion

#endif
    }
}
